package net.mbl.aicar.transform;

import java.util.HashMap;
import java.util.Map;

public class RecDataHandler implements IRecDataHandler {

	Map<Integer,IOperation> operationMap = new HashMap<Integer,IOperation>();
	public int headLen=2;
	public byte[] headByte = new byte[]{'@','@'};
	public int opCodeStart=2;
	public int opCodeEnd=2;
	public int lastOpCode;
	
	public void addOperation(int op,IOperation operation){
		operationMap.put(op, operation);
	}
	public void clearOperation(){
		operationMap.clear();
	}
	@Override
	public void handle(byte[] b,int len) {
		// TODO Auto-generated method stub
		if(b==null || b.length<len|| len<opCodeEnd || opCodeEnd<opCodeStart||opCodeStart<headLen)
			return;
		if(headLen>0 && headByte!=null && headByte.length>0){
			for(int i =0;i< headByte.length;i++){
				if(headByte[i]!=b[i])
					return;
			}
		}
		lastOpCode = 0;
		for(int i=0;i<opCodeStart-opCodeStart+1;i++){
			lastOpCode |= (0xff&b[opCodeStart+i])<<(i*8);
		}
		IOperation op = operationMap.get(lastOpCode);
		op.execute(b, opCodeEnd+1, len);
			
		System.out.println("handle "+len+" bytes :"+new String(b,0,len));
		
	}

}
